from flask import current_app

from models.StoreSubSysModels import Store, Collect, History, Shoppingcar
from utils.dataBase import db_session


class storeService:
    def __init__(self):
        pass

    # 商品列表
    def medcinelist(self):
        s = db_session.query(Store).all()
        list = []
        if s == None:
            dict['status'] = 0
            list.append(dict)
        else:
            for item in s:
                dict = {}
                dict['sid'] = item.sid
                dict['sname'] = item.sname
                dict['spic'] = item.spic
                dict['sprice'] = item.sprice
                list.append(dict)
        return list

    # 通过id查询到药品详情
    def findbyid(self, id):
        s = db_session.query(Store).filter_by(sid=id).first()
        list = []
        dict = {}
        if s == None:
            dict['status'] = 0
            list.append(dict)
        else:
            dict['sid'] = s.sid
            dict['sname'] = s.sname
            dict['spic'] = s.spic
            dict['sprice'] = s.sprice
            dict['sinfo'] = s.sinfo
            dict['snumber'] = s.snumber
            list.append(dict)
        return list

    # 通过用户id查询到收藏列表
    def collectlist(self, id):
        # 联合查询到商品列表
        obj = db_session.query(Store).join(Collect, Collect.sid == Store.sid).filter(Collect.uid == id).all()
        list = []
        if obj == None:
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['cid'] = item.collect[0].cid
                dict['sid'] = item.sid
                dict['sname'] = item.sname
                dict['spic'] = item.spic
                dict['sprice'] = item.sprice
                list.append(dict)
        return list

    # 添加收藏
    def addcollect(self, uid, sid):
        list = []
        dict = {}
        collect = Collect(uid=uid, sid=sid)
        db_session.add(collect)
        try:
            db_session.commit()
            dict['status'] = 1
            list.append(dict)
        except Exception as r:
            current_app.logger.info(r)
            dict['status'] = 0
            list.append(dict)
        return list

    # 取消收藏
    def delcollect(self, data):
        list = []
        dict = {}
        datalist = data['list']
        for item in datalist:
            #循环删除
            db_session.query(Collect).filter(Collect.cid == item).delete()
        try:
            db_session.commit()
            dict['status'] = 1
            list.append(dict)
        except Exception as r:
            current_app.logger.info(r)
            dict['status'] = 0
            list.append(dict)
        return list

    # 用户的购买记录
    def history(self, id):
        # 联合查询到商品列表
        obj = db_session.query(History).filter(History.uid == id).all()
        list = []
        if obj == None:
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['sid'] = item.store.sid
                dict['sname'] = item.store.sname
                dict['spic'] = item.store.spic
                dict['hsprice'] = item.hprice
                dict['hnumber'] = item.hnumber
                dict['time'] = item.time
                list.append(dict)
        return list

    # 购物车
    def shoppingcar(self, id):
        obj = db_session.query(Shoppingcar).filter(Shoppingcar.uid == id).all()
        list = []
        if obj == None:
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['scid'] = item.scid
                dict['sname'] = item.store.sname
                dict['spic'] = item.store.spic
                dict['sprice'] = item.store.sprice
                list.append(dict)
        return list

    # 添加购物车
    def addshoppingcar(self, uid, sid):
        list = []
        dict = {}
        shoppingcar = Shoppingcar(uid=uid, sid=sid)
        db_session.add(shoppingcar)
        try:
            db_session.commit()
            dict['status'] = 1
            list.append(dict)
        except Exception as r:
            current_app.logger.info(r)
            dict['status'] = 0
            list.append(dict)
        return list

    # 从购物车中删除
    def delshoppingcar(self, data):
        list = []
        dict = {}
        datalist = data['list']
        for item in datalist:
            # 循环删除
            db_session.query(Shoppingcar).filter(Shoppingcar.scid == item).delete()
        try:
            db_session.commit()
            dict['status'] = 1
            list.append(dict)
        except Exception as r:
            current_app.logger.info(r)
            dict['status'] = 0
            list.append(dict)
        return list

    # 搜索商品
    def search(self, str):
        express = "%" + str + "%"
        obj = db_session.query(Store).filter(Store.sname.like(express)).all()
        list = []
        if obj == None:
            dict['status'] = 0
            list.append(dict)
        else:
            for item in obj:
                dict = {}
                dict['sid'] = item.sid
                dict['sname'] = item.sname
                dict['spic'] = item.spic
                dict['sprice'] = item.sprice
                list.append(dict)
        return list
